package myClient.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.stereotype.Service;
import protobuf.protoClass.ProtoMsg;

/**
 * 处理服务器 Server发来的信息，过滤出 {@link ProtoMsg.MessageRequest}类型信息
 */

@ChannelHandler.Sharable
@Service("ChatMsgHandler")
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //判断消息实例
        if (!(msg instanceof ProtoMsg.Message protoMsg_Message)) {
            super.channelRead(ctx, msg);
            return;
        }
        //判断类型
        ProtoMsg.HeadType headType = protoMsg_Message.getType();
        if (!headType.equals(ProtoMsg.HeadType.MESSAGE_REQUEST)) {
            super.channelRead(ctx, msg);
            return;
        }
        ProtoMsg.MessageRequest messageRequest = protoMsg_Message.getMessageRequest();
        String content = messageRequest.getContent();
        String uid = messageRequest.getFrom();

        System.out.println(" 收到消息 from uid:" + uid + " -> " + content);
    }

}
